package com.yf.exam.modules.shiro.aop;


import com.yf.exam.aspect.utils.InjectUtils;
import com.yf.exam.core.utils.IpUtils;
import com.yf.exam.core.utils.SpringUtils;
import com.yf.exam.modules.Constant;
import com.yf.exam.modules.common.redis.service.RedisService;
import com.yf.exam.modules.shiro.jwt.JwtToken;
import com.yf.exam.modules.shiro.jwt.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 鉴权登录拦截器
 *
 * @author bool
 */


@Slf4j
public class JwtFilter extends BasicHttpAuthenticationFilter {


    /**
     * 执行登录认证
     *
     * @param request
     * @param response
     * @param mappedValue
     * @return
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        try {

            if(!executeLogin(request, response)){
                // 写出统一错误信息
                InjectUtils.restError((HttpServletResponse) response);
                return false;

            }


            return  true;
        } catch (Exception e) {
            // 写出统一错误信息
            InjectUtils.restError((HttpServletResponse) response);
            return false;
        }
    }


    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;


        String token = httpServletRequest.getHeader(Constant.TOKEN);

        String username = JwtUtils.getUsername(token);

        RedisService redisService = SpringUtils.getBean(RedisService.class);


        System.out.println("取出的 用户名:"+Constant.USER_SESSION_KEY+Constant.USER_NAME_KEY + username+" 和 token:"+token);

        String redisToken = redisService.getString(Constant.USER_SESSION_KEY + username);

        System.out.println("redisToken"+redisToken);
        System.out.println("token:"+token);
        System.out.println("redisToken.equals(token):"+redisToken.equals(token));

        if (!redisToken.equals(token) ){

            System.out.println("用户token和redis 中的对不上");
                throw new RuntimeException("ip:"+IpUtils.extractClientIp(httpServletRequest)+"登录了 该用户");
           // return   false;
        }


        JwtToken jwtToken = new JwtToken(token);
		// 提交给realm进行登入，如果错误他会抛出异常并被捕获
		getSubject(request, response).login(jwtToken);
		// 如果没有抛出异常则代表登入成功，返回true
		return true;
	}


    public static void main(String[] args) {

        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDk0NjcwNDYsInVzZXJuYW1lIjoiYWRtaW4ifQ.tioBX2G3wAgnKSZKdWCf-kZebMA0LWTrq12cQtGBiSA";


        System.out.println(token.equals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDk0NjcwNDYsInVzZXJuYW1lIjoiYWRtaW4ifQ.tioBX2G3wAgnKSZKdWCf-kZebMA0LWTrq12cQtGBiSA"));


    }


}
